//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmBatesSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Processes/BatesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmQuantoHelper.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (Cephei::QL::Processes::IBatesProcess^ process, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integroIntegrationOrder, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^>^ quantoHelper) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CBatesProcess^ _Cprocess;
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    CFdmQuantoHelper^ _CquantoHelper;
    try
    {
#ifdef HANDLE
        _phFdmBatesSolver = NULL;
#endif
        _Cprocess = safe_cast<CBatesProcess^> (process);
        _Cprocess->Lock();
        Handle<QuantLib::BatesProcess>& _process = static_cast<Handle<QuantLib::BatesProcess>&> (_Cprocess->GetHandle ()); 
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        QuantLib::Size _integroIntegrationOrder = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (integroIntegrationOrder) ? (QuantLib::Size)ValueHelper::Convert (integroIntegrationOrder->Value) : 12); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^>::IsSome::get (quantoHelper))
        {
            _CquantoHelper = safe_cast<CFdmQuantoHelper^> (quantoHelper->Value);
            _CquantoHelper->Lock();
        }
        Handle<QuantLib::FdmQuantoHelper>& _quantoHelper = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^>::IsSome::get (quantoHelper) ? static_cast<Handle<QuantLib::FdmQuantoHelper>&> (_CquantoHelper->GetHandle ()) : Handle<QuantLib::FdmQuantoHelper>()); //1
        _ppFdmBatesSolver = new boost::shared_ptr<QuantLib::FdmBatesSolver> (new QuantLib::FdmBatesSolver ( _process,  _solverDesc,  _schemeDesc,  _integroIntegrationOrder,  _quantoHelper ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
        if (_CquantoHelper != nullptr) _CquantoHelper->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (boost::shared_ptr<QuantLib::FdmBatesSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBatesSolver = NULL;
#endif
	_ppFdmBatesSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (QuantLib::FdmBatesSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmBatesSolver = NULL;
#endif
	_ppFdmBatesSolver = new boost::shared_ptr<QuantLib::FdmBatesSolver> (&childNative);
    
    _FdmBatesSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (CFdmBatesSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBatesSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBatesSolver = new boost::shared_ptr<QuantLib::FdmBatesSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBatesSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBatesSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (QuantLib::Handle<QuantLib::FdmBatesSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBatesSolver = &childNative;
	_ppFdmBatesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBatesSolver>>(childNative.currentLink());
    
    _FdmBatesSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (QuantLib::Handle<QuantLib::FdmBatesSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBatesSolver = &childNative;
	_ppFdmBatesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBatesSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::CFdmBatesSolver (QuantLib::FdmBatesSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBatesSolver = NULL;
#endif
	_ppFdmBatesSolver = new boost::shared_ptr<QuantLib::FdmBatesSolver> (new QuantLib::FdmBatesSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::~CFdmBatesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmBatesSolver != NULL)
    {
	    delete _ppFdmBatesSolver;
        _ppFdmBatesSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::!CFdmBatesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmBatesSolver != NULL)
    {
	    delete _ppFdmBatesSolver;
    }
}
QuantLib::FdmBatesSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::GetReference ()
{
    if (_ppFdmBatesSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmBatesSolver;
}
boost::shared_ptr<QuantLib::FdmBatesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::GetShared ()
{
    if (_ppFdmBatesSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmBatesSolver;
}
QuantLib::FdmBatesSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::GetPointer ()
{
    if (_ppFdmBatesSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmBatesSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBatesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::GetHandle ()
{
	if (_phFdmBatesSolver == NULL)
	{
		_phFdmBatesSolver = new Handle<QuantLib::FdmBatesSolver> (*_ppFdmBatesSolver);
	}
	return *_phFdmBatesSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::HasNative () 
{
	return (_ppFdmBatesSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::DeltaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBatesSolver)->deltaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::GammaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBatesSolver)->gammaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::ThetaAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBatesSolver)->thetaAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver::ValueAt (Double s, Double v)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBatesSolver)->valueAt ( _s,  _v );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmBatesSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBatesSolver_Factory::Create (Cephei::QL::Processes::IBatesProcess^ process, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integroIntegrationOrder, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^>^ quantoHelper)
{
    return REFNEW CFdmBatesSolver ( process,  solverDesc,  schemeDesc,  integroIntegrationOrder,  quantoHelper);
}
